#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"

struct student {
    char name[20];
    int age;
    struct list_head list;
};

int main()
{
    struct list_head stu_head = {&stu_head, &stu_head};

    struct student *stu1;
    stu1 = malloc(sizeof(*stu1));
    strcpy(stu1->name, "jack");
    stu1->age = 20;

    struct student *stu2;
    stu2 = malloc(sizeof(*stu2));
    strcpy(stu2->name, "bob");
    stu2->age = 30;

    struct student *stu3;
    stu3 = malloc(sizeof(*stu3));
    strcpy(stu3->name, "bobc");
    stu3->age = 31;

    list_add(&stu1->list, &stu_head);
    list_add(&stu2->list, &stu_head);
    list_add(&stu3->list, &stu_head);

    struct student *stu;
    list_for_each_entry(stu, &stu_head, list) {
        printf("stu->name=%s\n",stu->name);
    }

    /*手动实现从list_head访问student*/
    struct list_head *mptr = stu_head.next;

    unsigned long long offset = (unsigned long long)(&(((struct student *)0)->list));

    mptr = (unsigned long long)mptr - offset;
    
    stu = (struct student *)mptr;

    printf("stu = %s %p\n", stu->name, stu);
    printf("stu1 = %p\n", stu1);
    printf("stu2 = %p\n", stu2);
    printf("stu3 = %p\n", stu3);
    return 0;
}